import os
import logging
from quote_fetcher import fetch_quotes
from plot_generator import generate_plot, generate_and_store_plot
from story_generator import generate_story
from story_reviewer import review_story, Rule
from text_polisher import polish_text

# 确保日志目录存在
os.makedirs("logs", exist_ok=True)

# 设置全局日志配置
logging.basicConfig(
    level=logging.INFO,  # 设置日志级别
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',  # 设置日志格式
    handlers=[
        logging.FileHandler('logs/kongming.log'),  # 写入到全局日志文件
        logging.StreamHandler()  # 同时输出到控制台
    ]
)

logger = logging.getLogger(__name__)


def main():
    logger.info("Starting the project workflow.")

    # 1. 收集名言
    logger.info("Fetching quotes...")
    fetch_quotes()

    # 2. 生成情节
    logger.info("Generating plot...")
    plot = generate_plot("科幻背景下的人工智能起义")
    if plot:
        logger.info(f"Generated plot: {plot}")

    # 3. 创作故事
    if plot:
        logger.info("Generating story...")
        story = generate_story(plot, ["Alice", "Bob"])
        if story:
            logger.info(f"Generated story: {story}")

    # 4. 审核故事
    if story:
        logger.info("Reviewing story...")
        rules = [Rule("故事必须包含'起义'", lambda x: "起义" in x)]
        if review_story(story, rules):
            logger.info("Story passed review.")

            # 5. 润色故事
            logger.info("Polishing story...")
            polished_story = polish_text(story)
            if polished_story:
                logger.info("Story polished successfully.")
                print("Final Story: ", polished_story)


if __name__ == "__main__":
    main()
